home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Toolbox / OtherResInfo-MungeDeamon / MungeDeamonMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-22  |  6.1 KB  |  142 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #   Apple Developer Technical Support
  4. #
  5. #   MungeDeamon
  6. #   A small faceless background-only application for looking at other folks resources
  7. #
  8. #   MungeDeamonMain.c  -   C Source
  9. #
  10. #   Copyright © 1992 Apple Computer, Inc.
  11. #   All rights reserved.
  12. #
  13. #   Versions:   
  14. #               1.0               12/92       C.K. Haun <TR>
  15. #   
  16. #
  17. #    This is a faceless background process who's whole mission in
  18. #    life is to open resource forks, get information from them, and
  19. #     pass that information back to whoever asked for it.
  20. #    I wrote it to show one handy use of a background-only application,
  21. #    manipulating other people's resource forks in your application can often 
  22. #    cause you a lot of problems, you can inadvertantly load one of
  23. #    their resources instead of your own (bad, imagine running someone elses CODE
  24. #    segment instead of what you expected).  Also, you may not be sure
  25. #    if you can close the resource fork after you opened it (closing the resource fork
  26. #    of a running application is Not Good), and that means that their resource map
  27. #    will forever live in your heap, taking up memory and confusion.
  28. #    Using MungeDeamon to do the work for you gets rid of most of these problems.
  29. #    First, since MungeDeamon only has one resource it cares about, it's own MAIN
  30. #    code resource which is _always_ in memory, you don't need to worry about
  31. #    accidentally loading any resources.
  32. #     Second, any extra memory use is in MungeDeamon's heap, not the main app.
  33. #    Third, I have a flag in the request that can tell MungeDeamon to 
  34. #    quit as soon as it's done, letting the Process Manager handle closing forks
  35. #    and also immediatly cleaning up any memory needed in this operation.
  36. #    And so on, there are lots of handy things you can do with a background-only app.
  37. #
  38. #   The main things to note are....
  39. #   1)  Look at the SIZE resource for the flag settings you'll
  40. #       need to let the Finder™ know that you only want to work
  41. #       in the background.
  42. #   2)  Notice that you really do have your own heap and your own
  43. #       event loop.  You can do anything a foreground application
  44. #       can do, send AppleEvents, PPC stuff, anything else you'd
  45. #       like EXCEPT a graphic interface.
  46. #   3)  NOTE that no managers are started up.  You cannot start up
  47. #        Window, Menu, Dialogs, or anything else that 
  48. #       deals with the graphic interface.  Just leave them out.
  49. #        You CAN start up QuickDraw if you want to do some graphic 
  50. #        processing, but DO NOT draw to the screen, only use things 
  51. #        like offscreen grafports or GWorlds.  Or you may 
  52. #        want to start it to use Random().....
  53. #
  54. #   Of course, a backgrounder can be launched from the Finder™ 
  55. #   with a double-click.  However, if you'd like the backgrounder
  56. #   to perform some useful service for your main application, driver,
  57. #   DA, or whatever, you will want it running all the time.
  58. #   The BEST way to insure this is to install the backgrounder in the
  59. #   StartUp Items folder in the system folder.  This will insure that
  60. #   it is always launched, and it will also reduce memory fragmentation
  61. #   since it will be installed at startup time.  You can search for it
  62. #   when you need it with IPCListPorts (see the PPC toolbox documentation
  63. #    or the AECDEV code).
  64. #   Optionally, you can use the LaunchApplication trap to launch it 
  65. #   when you need it, and kill it when you're done.  
  66. #   And remember, it's a backgrounder, you can't see it.  To kill it
  67. #   use TaskIt or ProcDoggie.
  68. #
  69. #   ---------------------------------------------------------------
  70. #   Use this sample as a starting point, and adapt its' routines to 
  71. #   meet the specific needs of your project.  
  72. #   This sample will grow as more edition types are implemented,
  73. #   periodically check AppleLink for a later, expanded sample.
  74. #   This application is an example of the form of a Macintosh 
  75. #   application; it is NOT a template. It is NOT intended to be 
  76. #   used as a foundation for the next world-class, best-selling, 
  77. #   600K application. A stick figure drawing of the human body may 
  78. #   be a good example of the form for a painting, but that does not 
  79. #   mean it should be used as the basis for the next Mona Lisa.
  80. #
  81. #
  82. ------------------------------------------------------------------------------*/
  83.  
  84.  
  85. #include "MungeDeamon.h"
  86. Boolean gQuit = false;
  87. EventRecord ERecord;
  88.  
  89. Boolean gHasAppleEvents;
  90. unsigned long gMySleep = 200;  /* Long time.  Change this if  */
  91. /* you'd like to do null processing. Just keep in mind that the */
  92. /* user does NOT know that you exisist, and if you are eating */
  93. /* up a bunch of time the user will not know why his or her */
  94. /* machine is slowing down. */
  95. unsigned long quitTime;
  96. ProcessSerialNumber myPSN;
  97. void SetQuitTime(void);
  98.  
  99. short gOurResRef;   /* our resource file reference number */
  100.  
  101. main()
  102. {
  103.     /* We are NOT initializing any managers other than QuickDraw.  We're in the background, with no */
  104.     /* face, we can't use windows or dialogs or menus.  If you need to talk to the */
  105.     /* user you can post a notification, or launch an application to comunicate */
  106.     /* Passing an AppleEvent in the launchapplication trap could do the */
  107.     /* communication for you. */
  108.     /* no nothing but events */
  109.     /*  Why am I calling InitGraf here? Well, I'm a background-only */
  110.     /* app, but I use the AppleEvent manager and ask it to auto-generate */
  111.     /* return and transaction IDs.  AppleEvent Manager uses Random() to */
  112.     /* do this, so I have to have QD started */
  113.     gOurResRef = CurResFile();
  114.  
  115.     UnloadSeg((Ptr)_DataInit);                              /* throw out setup code */
  116.  
  117.     MaxApplZone();
  118.     InitGraf((Ptr)&qd.thePort); 
  119.     InitAEStuff();
  120.     GetCurrentProcess(&myPSN);
  121.     /* no nothing but high level events */
  122.     while (gQuit == false) {
  123.         WaitNextEvent(highLevelEventMask, &ERecord, gMySleep, 0);
  124.         /* if any event other than a nul happens, reset the timeout */
  125.         if(ERecord.what !=nullEvent)SetQuitTime();
  126.  
  127.         if (ERecord.what == kHighLevelEvent)
  128.         DoHighLevel(&ERecord);
  129.         if(quitTime < TickCount())gQuit = true;
  130.         }
  131. }
  132. /* I hate having to kill deamons all the time, so this one */
  133. /* self-destructs after 10 minutes of non-use */
  134. void SetQuitTime(void)
  135. {
  136. quitTime = TickCount() + ( 60 * 60 * 10  );
  137.  
  138.  
  139. }
  140.  
  141.  
  142.